/**
 * @description 用户关系 controller
 * @author 刘煎蛋
 */

const { ErrorModel, SuccessModel } = require("../model/ResModel")
const { addFollowerFailInfo, deleteFollowerFailInfo } = require("../model/ErrorInfo")
const { 
    addFollower, 
    deleteFollower,
    getUsersByFollower,
    getFollowersByUser
} = require('../services/user-relation')

/**
 * 根据userid获取粉丝列表
 */
async function getFans( userId ) {
    const { count, userList } = await getUsersByFollower( userId );

    return new SuccessModel({
        count,
        fansList: userList
    })
}

/**
 * 获取关注人列表
 * @param {*} userId 用户id 
 */
async function getFollowers( userId ) {
    const { count, userList } = await getFollowersByUser( userId );

    return new SuccessModel({
        count,
        follwersList: userList
    })
}

/**
 * 关注
 * @param {*} myUserId 当前登录的用户id
 * @param {*} curUserId 要背关注的用户id
 */
async function follow( myUserId, curUserId ) {
    try {
        await addFollower( myUserId, curUserId );
        return new SuccessModel();
    } catch( ex ) {
        return new ErrorModel(addFollowerFailInfo);
    }
}

/**
 * 取消关注
 * @param {*} myUserId 当前登录的用户id
 * @param {*} curUserId 要取消关注的用户id
 */
async function unFollow( myUserId, curUserId ) {
    const result = await deleteFollower( myUserId, curUserId );
    if ( result ) {
        return new SuccessModel();
    }
    return new ErrorModel(deleteFollowerFailInfo);
}

module.exports = {
    follow,
    unFollow,
    getFans,
    getFollowers
}